package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)

type SysDictionary struct {
	BaseModel
	ParentCode  string `orm:"column(parent_code);description(上级编码)" json:"parentCode"`
	DicCode     string `orm:"column(dic_code);description(编码)" json:"dicCode"`
	DicKey      string `orm:"column(dic_key);description(key)" json:"dicKey"`
	DicValue    string `orm:"column(dic_value);description(值)" json:"dicValue"`
	Description string `orm:"column(description);description(字典描述)" json:"description"`
	Sort        int    `orm:"column(sort);description(排序)" json:"sort"`
	Enabled     int    `orm:"column(enabled);description(启用状态,<0:无效；>=0:有效)" json:"enabled"`
}

func init() {
	orm.RegisterModel(new(SysDictionary))
}

// SearchField 定义模型的可搜索字段
func (*SysDictionary) SearchField() []string {
	return []string{"dic_code", "dic_key", "parent_code", "dic_value"}
}

// WhereField 定义模型可作为条件的字段
func (*SysDictionary) WhereField() []string {
	return []string{"id", "status", "enabled"}
}

// SimpleField 简略查询字段
func (*SysDictionary) SimpleField() []string {
	return []string{"id", "parent_code", "dic_code", "dic_key", "dic_value"}
}

func (m *SysDictionary) TableName() string {
	return "sys_dictionary"
}

// AddSysDictionary insert a new SysDictionary into database and returns
// last inserted Id on success.
func AddSysDictionary(m *SysDictionary, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetSysDictionaryById retrieves SysDictionary by Id. Returns error if
// Id doesn't exist
func GetSysDictionaryById(id int64) (v *SysDictionary, err error) {
	o := orm.NewOrm()
	v = &SysDictionary{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(SysDictionary)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateSysDictionaryById updates SysDictionary by Id and returns error if
// the record to be updated doesn't exist
func UpdateSysDictionaryById(m *SysDictionary, userId int64) (err error) {
	o := orm.NewOrm()
	v := SysDictionary{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteSysDictionary deletes SysDictionary by Id and returns error if
// the record to be deleted doesn't exist
func DeleteSysDictionary(id int64) (err error) {
	o := orm.NewOrm()
	v := SysDictionary{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&SysDictionary{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroySysDictionary Destroy SysDictionary by Id and returns error
func DestroySysDictionary(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := SysDictionary{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
